<!DOCTYPE html>


<html theme="dark" showBanner="true" hasBanner="false" > 
<link href="https://cdn.staticfile.org/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet">
<link href="https://cdn.staticfile.org/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet">
<link href="https://cdn.staticfile.org/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet">
<script src="/hexo-blog/js/color.global.min.js" ></script>
<script src="/hexo-blog/js/load-settings.js" ></script>
<head>
  <meta charset="utf-8">
  
  
  

  
  <title>@nestjsx/crud快速入门 | bressanone</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <link rel="preload" href="/hexo-blog/css/fonts/Roboto-Regular.ttf" as="font" type="font/ttf" crossorigin="anonymous">
  <link rel="preload" href="/hexo-blog/css/fonts/Roboto-Bold.ttf" as="font" type="font/ttf" crossorigin="anonymous">

  <meta name="description" content="「这是我参与2022首次更文挑战的第22天，活动详情查看：2022首次更文挑战」 介绍@nestjsx&#x2F;crud 是为NestJs服务的个“微框架”，能大大简化CRUD代码的编写。虽然官方自称为“微框架”，但个人觉得理解为“工具集”可能更合适。 项目初始化首先使用nest脚手架生成一个项目 12npm i -g @nestjs&#x2F;clinest new curd-demo  然后安装@ne">
<meta property="og:type" content="article">
<meta property="og:title" content="@nestjsx&#x2F;crud快速入门">
<meta property="og:url" content="http://example.com/2022/02/18/@nestjsx%20crud%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="bressanone">
<meta property="og:description" content="「这是我参与2022首次更文挑战的第22天，活动详情查看：2022首次更文挑战」 介绍@nestjsx&#x2F;crud 是为NestJs服务的个“微框架”，能大大简化CRUD代码的编写。虽然官方自称为“微框架”，但个人觉得理解为“工具集”可能更合适。 项目初始化首先使用nest脚手架生成一个项目 12npm i -g @nestjs&#x2F;clinest new curd-demo  然后安装@ne">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/2022/02/18/imgs/441d5fe819d241e0ae5c27a8dc5d05bb.png">
<meta property="og:image" content="http://example.com/2022/02/18/imgs/6d3566e0ddcd4bbdab3d5a39d04c918c.png">
<meta property="og:image" content="http://example.com/2022/02/18/imgs/071f7b1d75294faa9789f01815f25fa0.png">
<meta property="og:image" content="http://example.com/2022/02/18/imgs/e8e040971352443faa8d18bd39eb37fa.png">
<meta property="og:image" content="http://example.com/2022/02/18/imgs/0731bc95d135415e9b5ad45fae7a0e6d.png">
<meta property="article:published_time" content="2022-02-17T16:00:00.000Z">
<meta property="article:modified_time" content="2024-04-04T15:27:03.212Z">
<meta property="article:author" content="Zachary">
<meta property="article:tag" content="Node.js">
<meta property="article:tag" content="NestJS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/2022/02/18/imgs/441d5fe819d241e0ae5c27a8dc5d05bb.png">
  
  
    <link rel="icon" media="(prefers-color-scheme: light)" href="/hexo-blog/images/favicon-light-32.png" sizes="32x32">
    <link rel="icon" media="(prefers-color-scheme: light)" href="/hexo-blog/images/favicon-light-128.png" sizes="128x128">
    <link rel="icon" media="(prefers-color-scheme: light)" href="/hexo-blog/images/favicon-light-180.png" sizes="180x180">
    <link rel="icon" media="(prefers-color-scheme: light)" href="/hexo-blog/images/favicon-light-192.png" sizes="192x192">
    <link rel="icon" media="(prefers-color-scheme: dark)" href="/hexo-blog/images/favicon-dark-32.png" sizes="32x32">
    <link rel="icon" media="(prefers-color-scheme: dark)" href="/hexo-blog/images/favicon-dark-128.png" sizes="128x128">
    <link rel="icon" media="(prefers-color-scheme: dark)" href="/hexo-blog/images/favicon-dark-180.png" sizes="180x180">
    <link rel="icon" media="(prefers-color-scheme: dark)" href="/hexo-blog/images/favicon-dark-192.png" sizes="192x192">
  
  
<link rel="stylesheet" href="/hexo-blog/css/style.css">

<meta name="generator" content="Hexo 7.1.1"></head>

<body>
  
   
  <div id="main-grid" class="  ">
    <div id="nav" class=""  >
      <navbar id="navbar">
  <nav id="title-nav">
    <a href="/hexo-blog/">
      <div id="vivia-logo">
        <div class="dot"></div>
        <div class="dot"></div>
        <div class="dot"></div>
        <div class="dot"></div>
      </div>
      <div>bressanone </div>
    </a>
  </nav>
  <nav id="main-nav">
    
      <a class="main-nav-link" href="/hexo-blog/">主页</a>
    
      <a class="main-nav-link" href="/hexo-blog/archives">归档</a>
    
      <a class="main-nav-link" href="/hexo-blog/about">关于</a>
    
  </nav>
  <nav id="sub-nav">
    <a id="theme-btn" class="nav-icon">
      <span class="light-mode-icon"><svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 -960 960 960" width="20"><path d="M438.5-829.913v-48q0-17.452 11.963-29.476 11.964-12.024 29.326-12.024 17.363 0 29.537 12.024t12.174 29.476v48q0 17.452-11.963 29.476-11.964 12.024-29.326 12.024-17.363 0-29.537-12.024T438.5-829.913Zm0 747.826v-48q0-17.452 11.963-29.476 11.964-12.024 29.326-12.024 17.363 0 29.537 12.024t12.174 29.476v48q0 17.452-11.963 29.476-11.964 12.024-29.326 12.024-17.363 0-29.537-12.024T438.5-82.087ZM877.913-438.5h-48q-17.452 0-29.476-11.963-12.024-11.964-12.024-29.326 0-17.363 12.024-29.537t29.476-12.174h48q17.452 0 29.476 11.963 12.024 11.964 12.024 29.326 0 17.363-12.024 29.537T877.913-438.5Zm-747.826 0h-48q-17.452 0-29.476-11.963-12.024-11.964-12.024-29.326 0-17.363 12.024-29.537T82.087-521.5h48q17.452 0 29.476 11.963 12.024 11.964 12.024 29.326 0 17.363-12.024 29.537T130.087-438.5Zm660.174-290.87-34.239 32q-12.913 12.674-29.565 12.174-16.653-.5-29.327-13.174-12.674-12.673-12.554-28.826.12-16.152 12.794-28.826l33-35q12.913-12.674 30.454-12.674t30.163 12.847q12.709 12.846 12.328 30.826-.38 17.98-13.054 30.653ZM262.63-203.978l-32 34q-12.913 12.674-30.454 12.674t-30.163-12.847q-12.709-12.846-12.328-30.826.38-17.98 13.054-30.653l33.239-31q12.913-12.674 29.565-12.174 16.653.5 29.327 13.174 12.674 12.673 12.554 28.826-.12 16.152-12.794 28.826Zm466.74 33.239-32-33.239q-12.674-12.913-12.174-29.565.5-16.653 13.174-29.327 12.673-12.674 28.826-13.054 16.152-.38 28.826 12.294l35 33q12.674 12.913 12.674 30.454t-12.847 30.163q-12.846 12.709-30.826 12.328-17.98-.38-30.653-13.054ZM203.978-697.37l-34-33q-12.674-12.913-13.174-29.945-.5-17.033 12.174-29.707t31.326-13.293q18.653-.62 31.326 13.054l32 34.239q11.674 12.913 11.174 29.565-.5 16.653-13.174 29.327-12.673 12.674-28.826 12.554-16.152-.12-28.826-12.794ZM480-240q-100 0-170-70t-70-170q0-100 70-170t170-70q100 0 170 70t70 170q0 100-70 170t-170 70Zm-.247-82q65.703 0 111.475-46.272Q637-414.544 637-480.247t-45.525-111.228Q545.95-637 480.247-637t-111.475 45.525Q323-545.95 323-480.247t45.525 111.975Q414.05-322 479.753-322ZM481-481Z"/></svg></span>
      <span class="dark-mode-icon"><svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 -960 960 960" width="20"><path d="M480.239-116.413q-152.63 0-258.228-105.478Q116.413-327.37 116.413-480q0-130.935 77.739-227.435t206.304-125.043q43.022-9.631 63.87 10.869t3.478 62.805q-8.891 22.043-14.315 44.463-5.424 22.42-5.424 46.689 0 91.694 64.326 155.879 64.325 64.186 156.218 64.186 24.369 0 46.978-4.946 22.609-4.945 44.413-14.076 42.826-17.369 62.967 1.142 20.142 18.511 10.511 61.054Q807.174-280 712.63-198.206q-94.543 81.793-232.391 81.793Zm0-95q79.783 0 143.337-40.217 63.554-40.218 95.793-108.283-15.608 4.044-31.097 5.326-15.49 1.283-31.859.805-123.706-4.066-210.777-90.539-87.071-86.473-91.614-212.092-.24-16.369.923-31.978 1.164-15.609 5.446-30.978-67.826 32.478-108.282 96.152Q211.652-559.543 211.652-480q0 111.929 78.329 190.258 78.329 78.329 190.258 78.329ZM466.13-465.891Z"/></svg></span>
    </a>
    
    <div id="nav-menu-btn" class="nav-icon">
      <svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 -960 960 960" width="20"><path d="M177.37-252.282q-17.453 0-29.477-11.964-12.024-11.963-12.024-29.326t12.024-29.537q12.024-12.174 29.477-12.174h605.26q17.453 0 29.477 11.964 12.024 11.963 12.024 29.326t-12.024 29.537q-12.024 12.174-29.477 12.174H177.37Zm0-186.218q-17.453 0-29.477-11.963-12.024-11.964-12.024-29.326 0-17.363 12.024-29.537T177.37-521.5h605.26q17.453 0 29.477 11.963 12.024 11.964 12.024 29.326 0 17.363-12.024 29.537T782.63-438.5H177.37Zm0-186.217q-17.453 0-29.477-11.964-12.024-11.963-12.024-29.326t12.024-29.537q12.024-12.174 29.477-12.174h605.26q17.453 0 29.477 11.964 12.024 11.963 12.024 29.326t-12.024 29.537q-12.024 12.174-29.477 12.174H177.37Z"/></svg>
    </div>
  </nav>
</navbar>
<div id="nav-dropdown" class="hidden">
  <div id="dropdown-link-list">
    
      <a class="nav-dropdown-link" href="/hexo-blog/">主页</a>
    
      <a class="nav-dropdown-link" href="/hexo-blog/archives">归档</a>
    
      <a class="nav-dropdown-link" href="/hexo-blog/about">关于</a>
    
     
    </div>
</div>
<script>
  let dropdownBtn = document.getElementById("nav-menu-btn");
  let dropdownEle = document.getElementById("nav-dropdown");
  dropdownBtn.onclick = function() {
    dropdownEle.classList.toggle("hidden");
  }
</script>
    </div>
    <div id="sidebar-wrapper">
      <sidebar id="sidebar">
  
    <div class="widget-wrap">
  <div class="info-card">
    <div class="avatar">
      
      <div class="img-dim"></div>
    </div>
    <div class="info">
      <div class="username">Zachary </div>
      <div class="dot"></div>
      <div class="subtitle"> </div>
      <div class="link-list">
        
          <a class="link-btn" target="_blank" rel="noopener" href="https://github.com/zzy1661" title="GitHub"><i class="fa-brands fa-github"></i></a>
         
      </div>  
    </div>
  </div>
</div>

  
  <div class="sticky">
    
      


  <div class="widget-wrap">
    <div class="widget">
      <h3 class="widget-title">分类</h3>
      <div class="category-box">
            <a class="category-link" href="/hexo-blog/categories/%E5%89%8D%E7%AB%AF%E7%AC%94%E8%AE%B0/">
                前端笔记
                <div class="category-count">78</div>
            </a>
        
            <a class="category-link" href="/hexo-blog/categories/%E7%AE%97%E6%B3%95%E6%89%8B%E5%86%8C/">
                算法手册
                <div class="category-count">7</div>
            </a>
        </div>
    </div>
  </div>


    
      
  <div class="widget-wrap">
    <div class="widget">
      <h3 class="widget-title">标签</h3>
      <ul class="widget-tag-list" itemprop="keywords"><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Ant-Design/" rel="tag">Ant Design</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/CSS/" rel="tag">CSS</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Canvas/" rel="tag">Canvas</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Docker/" rel="tag">Docker</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/ECMAScript-6/" rel="tag">ECMAScript 6</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/JavaScript/" rel="tag">JavaScript</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Less/" rel="tag">Less</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/MobX/" rel="tag">MobX</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/MongoDB/" rel="tag">MongoDB</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/NPM/" rel="tag">NPM</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/NestJS/" rel="tag">NestJS</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Node-js/" rel="tag">Node.js</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Postman/" rel="tag">Postman</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/React-js/" rel="tag">React.js</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Redux/" rel="tag">Redux</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/TypeScript/" rel="tag">TypeScript</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/UML/" rel="tag">UML</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Webpack/" rel="tag">Webpack</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/Yeoman/" rel="tag">Yeoman</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/%E5%89%8D%E7%AB%AF/" rel="tag">前端</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/%E6%9E%B6%E6%9E%84/" rel="tag">架构</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" rel="tag">正则表达式</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/%E7%88%AC%E8%99%AB/" rel="tag">爬虫</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/%E7%AE%97%E6%B3%95/" rel="tag">算法</a></li><li class="widget-tag-list-item"><a class="widget-tag-list-link" href="/hexo-blog/tags/%E9%9D%A2%E8%AF%95/" rel="tag">面试</a></li></ul>
    </div>
  </div>


    
  </div>
</sidebar>
    </div>
    <div id="content-body">
       


<article id="post-@nestjsx crud快速入门" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  
    
   
  <div class="article-inner">
    <div class="article-main">
      <header class="article-header">
        
<div class="main-title-bar">
  <div class="main-title-dot"></div>
  
    
      <h1 class="p-name article-title" itemprop="headline name">
        @nestjsx/crud快速入门
      </h1>
    
  
</div>

        <div class='meta-info-bar'>
          <div class="meta-info">
  <time class="dt-published" datetime="2022-02-17T16:00:00.000Z" itemprop="datePublished">2022-02-18</time>
</div>
          <div class="need-seperator meta-info">
            <div class="meta-cate-flex">
  
  <a class="meta-cate-link" href="/hexo-blog/categories/%E5%89%8D%E7%AB%AF%E7%AC%94%E8%AE%B0/">前端笔记</a>
   
</div>
  
          </div>
          <div class="wordcount need-seperator meta-info">
            7.6k 词 
          </div>
        </div>
        
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo-blog/tags/NestJS/" rel="tag">NestJS</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo-blog/tags/Node-js/" rel="tag">Node.js</a></li></ul>

      </header>
      <div class="e-content article-entry" itemprop="articleBody">
        
          <p>「这是我参与2022首次更文挑战的第22天，活动详情查看：<a target="_blank" rel="noopener" href="https://juejin.cn/post/7052884569032392740" title="https://juejin.cn/post/7052884569032392740">2022首次更文挑战</a>」</p>
<h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>@nestjsx&#x2F;crud 是为NestJs服务的个“微框架”，能大大简化CRUD代码的编写。虽然官方自称为“微框架”，但个人觉得理解为“工具集”可能更合适。</p>
<h2 id="项目初始化"><a href="#项目初始化" class="headerlink" title="项目初始化"></a>项目初始化</h2><p>首先使用nest脚手架生成一个项目</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm <span class="selector-tag">i</span> -g <span class="keyword">@nestjs</span>/cli</span><br><span class="line">nest new curd-demo</span><br></pre></td></tr></table></figure>

<p>然后安装@nestjsx&#x2F;crud的依赖：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm i <span class="meta">@nestjsx</span>/crud <span class="keyword">class</span>-transformer <span class="keyword">class</span>-validator</span><br><span class="line">npm i <span class="meta">@nestjsx</span>/crud-typeorm <span class="meta">@nestjs</span>/typeorm typeorm</span><br></pre></td></tr></table></figure>

<p>@nestjsx&#x2F;crud和@nestjsx&#x2F;crud-typeorm分别提供了一个<code>@Crud()</code>装饰器和<code>TypeOrmCrudService</code>类，分别作用域controller和service。</p>
<h3 id="配置数据库连接"><a href="#配置数据库连接" class="headerlink" title="配置数据库连接"></a>配置数据库连接</h3><h3 id="配置TypeORM配置文件"><a href="#配置TypeORM配置文件" class="headerlink" title="配置TypeORM配置文件"></a>配置TypeORM配置文件</h3><p>在src目录下创建orm.js,这里需要注意配置<code>synchronize: false</code>,数据库同步等操作用TypeORM的命令。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* eslint-disable */</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = &#123;</span><br><span class="line">  <span class="attr">type</span>: <span class="string">&#x27;mysql&#x27;</span>,</span><br><span class="line">  <span class="attr">host</span>: <span class="string">&#x27;127.0.0.1&#x27;</span>,</span><br><span class="line">  <span class="attr">port</span>: <span class="number">3306</span>,</span><br><span class="line">  <span class="attr">username</span>: <span class="string">&#x27;fund&#x27;</span>,</span><br><span class="line">  <span class="attr">password</span>: <span class="string">&#x27;good_luck123&#x27;</span>,</span><br><span class="line">  <span class="attr">database</span>: <span class="string">&#x27;fund&#x27;</span>,</span><br><span class="line">  <span class="attr">synchronize</span>: <span class="literal">false</span>,</span><br><span class="line">  <span class="attr">entities</span>: [path.<span class="title function_">join</span>(__dirname, <span class="string">&#x27;./**/*.entity&#123;.ts,.js&#125;&#x27;</span>)],</span><br><span class="line">  <span class="attr">migrationsTableName</span>: <span class="string">&#x27;orm_migrations&#x27;</span>,</span><br><span class="line">  <span class="attr">migrations</span>: [<span class="string">&#x27;./ seeds.ts&#x27;</span>],</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="nest集成TypeORM"><a href="#nest集成TypeORM" class="headerlink" title="nest集成TypeORM"></a>nest集成TypeORM</h3><p>TypeOrmModule.forRoot可以不传参，它默认会从根路径下的TypeORM配置文件获取参数，但这里有个坑，这个api支持的参数要比TypeORM的参数更多，如果全都配置在TypeORM的配置文件中，forRoot会把那些TypeORM不支持的参数全部忽略。</p>
<p>因此这里有些实践是配置两个文件，一个是给TypeORM，用于TypeORM命令，一个是给nest的，用于连接数据库，@nestjsx&#x2F;crud的官方example就是这么做的。</p>
<p>不过可以变通一下，这里在src目录下再创建一个orm.config.ts文件,从orm.js中读取文件，然后进行合并，用作nest的数据库配置。</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">TypeOrmModuleOptions</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjs/typeorm&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; isNil &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjsx/util&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> config <span class="keyword">from</span> <span class="string">&#x27;./orm&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="attr">withCache</span>: <span class="title class_">TypeOrmModuleOptions</span> = &#123;</span><br><span class="line">  ...(config <span class="keyword">as</span> <span class="title class_">TypeOrmModuleOptions</span>),</span><br><span class="line">  <span class="attr">logging</span>: !<span class="title function_">isNil</span>(process.<span class="property">env</span>.<span class="property">TYPEORM_LOGGING</span>)</span><br><span class="line">    ? !!<span class="built_in">parseInt</span>(process.<span class="property">env</span>.<span class="property">TYPEORM_LOGGING</span>, <span class="number">10</span>)</span><br><span class="line">    : <span class="literal">true</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>然后在app.module.ts中配置如下</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">TypeOrmModule</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjs/typeorm&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; withCache &#125; <span class="keyword">from</span> <span class="string">&#x27;./orm.config&#x27;</span>;</span><br><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">imports</span>: [</span><br><span class="line">    <span class="title class_">TypeOrmModule</span>.<span class="title function_">forRoot</span>(withCache),</span><br><span class="line">    <span class="title class_">FundModule</span>,</span><br><span class="line">    <span class="title class_">AnnualProfitModule</span>,</span><br><span class="line">    <span class="title class_">RecentProfitModule</span>,</span><br><span class="line">  ],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AppModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>并设置tsconfig.json</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    ...</span><br><span class="line">    <span class="attr">&quot;allowJs&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="定义实体类"><a href="#定义实体类" class="headerlink" title="定义实体类"></a>定义实体类</h2><p>之后是根据项目创建实体类,这里需要用到TypeORM的<code>@Entity和@Column</code>,比如</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="type">Entity</span>, <span class="type">Column</span> &#125; from &#x27;typeorm&#x27;;</span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">BaseFund</span> <span class="keyword">extends</span> <span class="title">BaseEntity</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Column</span>(&#123; <span class="class"><span class="keyword">type</span></span>: &#x27;varchar&#x27;, length: <span class="number">6</span>, nullable: <span class="literal">false</span> &#125;)</span><br><span class="line">  code?: string;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">  <span class="meta">@Column</span>(&#123; <span class="class"><span class="keyword">type</span></span>: &#x27;varchar&#x27;, length: <span class="number">50</span>, nullable: <span class="literal">false</span> &#125;)</span><br><span class="line">  name?: string;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Entity</span>(&#x27;funds&#x27;)</span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">Fund</span> <span class="keyword">extends</span> <span class="title">BaseFund</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">  <span class="meta">@Column</span>(&#123; nullable: <span class="literal">true</span>, <span class="class"><span class="keyword">type</span></span>: &#x27;varchar&#x27;, length: <span class="number">10</span> &#125;)</span><br><span class="line">  fundType?: string; <span class="comment">//基金类型：股票型</span></span><br><span class="line"></span><br><span class="line">  <span class="meta">@Column</span>(&#123; nullable: <span class="literal">true</span>, <span class="class"><span class="keyword">type</span></span>: &#x27;float&#x27;, scale: <span class="number">4</span>, precision: <span class="number">9</span> &#125;)</span><br><span class="line">  price: number;</span><br><span class="line"> </span><br><span class="line">  <span class="meta">@Column</span>(&#123; nullable: <span class="literal">true</span> &#125;)</span><br><span class="line">  grade?: number; <span class="comment">//基金评级：</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><code>@Entity(&#39;funds&#39;)</code>表示这个实体类关联表funds，这里可以改为其他表名。<code>@Column()</code>表示这个字段对应数据库中的一个列。定义实体类的时候需要注意设置好数据类型，比如number类型的字段，默认生成的数据库字段是int类型，如果是float，需要设置它的精度。</p>
<h2 id="同步数据库"><a href="#同步数据库" class="headerlink" title="同步数据库"></a>同步数据库</h2><p>package.json中增加两个脚本：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line"><span class="attr">&quot;scripts&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;typeorm&quot;</span><span class="punctuation">:</span> <span class="string">&quot;cd src &amp;&amp; ts-node -r tsconfig-paths/register ../node_modules/typeorm/cli.js&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;db:sync&quot;</span><span class="punctuation">:</span> <span class="string">&quot;yarn typeorm schema:sync -f=orm&quot;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<p>需要注意这里TypeORM的配置文件位置不在根路径，名字也不是它默认的，因此先<code>cd src</code>，然后再<code>typeorm schema:sync -f=orm</code>,<code>-f</code>表示文件名是orm。</p>
<p>然后运行命令<code>yarn db:sync</code>，可以看到数据库表创建成功：</p>
<p><img src="./../imgs/441d5fe819d241e0ae5c27a8dc5d05bb.png" alt="image.png"></p>
<p><img src="./../imgs/6d3566e0ddcd4bbdab3d5a39d04c918c.png" alt="image.png"></p>
<h2 id="创建controller、service和module"><a href="#创建controller、service和module" class="headerlink" title="创建controller、service和module"></a>创建controller、service和module</h2><p>service:</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Injectable</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjs/common&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">InjectRepository</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjs/typeorm&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">TypeOrmCrudService</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjsx/crud-typeorm&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">AnnualProfit</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;./annualProfit.entity&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Injectable</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AnnualProfitService</span> <span class="keyword">extends</span> <span class="title class_ inherited__">TypeOrmCrudService</span>&lt;<span class="title class_">AnnualProfit</span>&gt; &#123;</span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params"><span class="meta">@InjectRepository</span>(AnnualProfit) repo</span>) &#123;</span><br><span class="line">    <span class="variable language_">super</span>(repo);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">async</span> <span class="title function_">updateScaleByCode</span>(<span class="params">code, info: <span class="built_in">Array</span>&lt;[<span class="built_in">number</span>, <span class="built_in">number</span>]&gt;</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">await</span> <span class="title class_">Promise</span>.<span class="title function_">all</span>(</span><br><span class="line">      info.<span class="title function_">map</span>(<span class="function">(<span class="params">[year, scale]</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">repo</span>.<span class="title function_">update</span>(&#123; code, year &#125;, &#123; scale &#125;);</span><br><span class="line">      &#125;),</span><br><span class="line">    ).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span></span><br><span class="line">      res.<span class="title function_">reduce</span>(<span class="function">(<span class="params">pre, cur</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> pre + cur.<span class="property">affected</span>;</span><br><span class="line">      &#125;, <span class="number">0</span>),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>service需要继承<code>TypeOrmCrudService</code>，这个类有很多基本的CRUD操作api,可以直接用。service中需要自定义处理逻辑时可以使用TypeORM的responsitory对象。</p>
<p><img src="./../imgs/071f7b1d75294faa9789f01815f25fa0.png" alt="image.png"> controller:</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Crud</span>, <span class="title class_">CrudController</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjsx/crud&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">BaseRes</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;src/base-entity&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">AnnualProfit</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;./annualProfit.entity&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">AnnualProfitService</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;./annualProfit.service&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Crud</span>(&#123;</span><br><span class="line">  <span class="attr">model</span>: &#123;</span><br><span class="line">    <span class="attr">type</span>: <span class="title class_">AnnualProfit</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">&#x27;/annualProfit&#x27;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AnnualProfitController</span> <span class="keyword">implements</span> <span class="title class_">CrudController</span>&lt;<span class="title class_">AnnualProfit</span>&gt; &#123;</span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params"><span class="keyword">public</span> service: AnnualProfitService</span>) &#123;&#125;</span><br><span class="line">  <span class="meta">@Post</span>(<span class="string">&#x27;updateScale&#x27;</span>)</span><br><span class="line">  <span class="keyword">async</span> <span class="title function_">updateScale</span>(<span class="params"></span></span><br><span class="line"><span class="params">    <span class="meta">@Body</span>() body: &#123; code: <span class="built_in">string</span>; scale: <span class="built_in">Array</span>&lt;[<span class="built_in">number</span>, <span class="built_in">number</span>]&gt; &#125;,</span></span><br><span class="line"><span class="params">  </span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">service</span></span><br><span class="line">      .<span class="title function_">updateScaleByCode</span>(body.<span class="property">code</span>, body.<span class="property">scale</span>)</span><br><span class="line">      .<span class="title function_">then</span>(<span class="function"><span class="params">result</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">BaseRes</span>(<span class="number">0</span>, &#123; <span class="attr">data</span>: result, <span class="attr">message</span>: <span class="string">`affected <span class="subst">$&#123;result&#125;</span>`</span> &#125;);</span><br><span class="line">      &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>controller需要用<code>@Crud</code>装饰，这会直接生成基本的CRUD resultful 请求。当然controller中也可以自定义其他请求。</p>
<p>module</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Module</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjs/common&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">TypeOrmModule</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjs/typeorm&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">AnnualProfit</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;./annualProfit.entity&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">AnnualProfitService</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;./annualProfit.service&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">AnnualProfitController</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;./annualProfit.controller&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">imports</span>: [<span class="title class_">TypeOrmModule</span>.<span class="title function_">forFeature</span>([<span class="title class_">AnnualProfit</span>])],</span><br><span class="line">  <span class="attr">providers</span>: [<span class="title class_">AnnualProfitService</span>],</span><br><span class="line">  <span class="attr">exports</span>: [<span class="title class_">AnnualProfitService</span>],</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">AnnualProfitController</span>],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AnnualProfitModule</span> &#123;&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="生成swagger"><a href="#生成swagger" class="headerlink" title="生成swagger"></a>生成swagger</h2><p>加上swagger后可以看到一共生成了哪些接口。</p>
<p>对controller增加<code>@ApiTags()</code>,并对自定义请求的dto设置<code>@ApiProperty()</code>和<code>@ApiResponse()</code></p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ApiTags</span>(<span class="string">&#x27;annualProfit&#x27;</span>)</span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">&#x27;/annualProfit&#x27;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AnnualProfitController</span> <span class="keyword">implements</span> <span class="title class_">CrudController</span>&lt;<span class="title class_">AnnualProfit</span>&gt; &#123;</span><br><span class="line"> ...</span><br><span class="line"> <span class="meta">@ApiResponse</span>(&#123; <span class="attr">type</span>: <span class="title class_">BaseRes</span> &#125;)</span><br><span class="line">  <span class="meta">@Post</span>(<span class="string">&#x27;updateScale&#x27;</span>)</span><br><span class="line">  <span class="keyword">async</span> <span class="title function_">updateScale</span>(<span class="params"><span class="meta">@Body</span>() body: UpdateScaleDto</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">service</span></span><br><span class="line">      .<span class="title function_">updateScaleByCode</span>(body.<span class="property">code</span>, body.<span class="property">scale</span>)</span><br><span class="line">      .<span class="title function_">then</span>(<span class="function"><span class="params">result</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">BaseRes</span>(<span class="number">0</span>, &#123; <span class="attr">data</span>: result, <span class="attr">message</span>: <span class="string">`affected <span class="subst">$&#123;result&#125;</span>`</span> &#125;);</span><br><span class="line">      &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>对entity增加<code>@ApiProperty()</code></p>
<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">import</span> &#123; <span class="selector-tag">ApiProperty</span> &#125; <span class="selector-tag">from</span> &#x27;@<span class="selector-tag">nestjs</span>/<span class="selector-tag">swagger</span>&#x27;;</span><br><span class="line"></span><br><span class="line">@<span class="selector-tag">Entity</span>(<span class="string">&#x27;annual_profit&#x27;</span>)</span><br><span class="line"><span class="selector-tag">export</span> <span class="selector-tag">class</span> <span class="selector-tag">AnnualProfit</span> <span class="selector-tag">extends</span> <span class="selector-tag">BaseFund</span> &#123;</span><br><span class="line">  <span class="variable">@ApiProperty</span>(&#123; <span class="attribute">type</span>: <span class="string">&#x27;number&#x27;</span>, <span class="attribute">name</span>: <span class="string">&#x27;year&#x27;</span>, <span class="attribute">description</span>: <span class="string">&#x27;年份&#x27;</span> &#125;)</span><br><span class="line">  <span class="variable">@Column</span>(&#123; <span class="attribute">nullable</span>: true &#125;)</span><br><span class="line">  <span class="attribute">year</span>: number; <span class="comment">// 年份</span></span><br><span class="line"> ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>修改main.ts</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">bootstrap</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">const</span> app = <span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="title function_">create</span>(<span class="title class_">AppModule</span>);</span><br><span class="line">  <span class="keyword">const</span> options = <span class="keyword">new</span> <span class="title class_">DocumentBuilder</span>()</span><br><span class="line">    .<span class="title function_">setTitle</span>(<span class="string">&#x27;fund-curd&#x27;</span>)</span><br><span class="line">    .<span class="title function_">setDescription</span>(<span class="string">&#x27;fund-curd&#x27;</span>)</span><br><span class="line">    .<span class="title function_">setVersion</span>(<span class="string">&#x27;1.0&#x27;</span>)</span><br><span class="line">    .<span class="title function_">build</span>();</span><br><span class="line">  <span class="keyword">const</span> <span class="variable language_">document</span> = <span class="title class_">SwaggerModule</span>.<span class="title function_">createDocument</span>(app, options);</span><br><span class="line">  <span class="title class_">SwaggerModule</span>.<span class="title function_">setup</span>(<span class="string">&#x27;docs&#x27;</span>, app, <span class="variable language_">document</span>);</span><br><span class="line">  <span class="keyword">await</span> app.<span class="title function_">listen</span>(process.<span class="property">env</span>.<span class="property">PORT</span> || <span class="number">3000</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">bootstrap</span>();</span><br></pre></td></tr></table></figure>

<p>访问页面<code>http://localhost:3000/docs</code>,图中框住的部分就是@nestjsx&#x2F;crud给我们生成的restful接口，另一个是我们自己设置的接口。</p>
<p><img src="./../imgs/e8e040971352443faa8d18bd39eb37fa.png" alt="image.png"></p>
<h2 id="nestjsx-crud-request"><a href="#nestjsx-crud-request" class="headerlink" title="@nestjsx&#x2F;crud-request"></a>@nestjsx&#x2F;crud-request</h2><p>截图中可以看到基本的增删改查、批量创建、分页查询它都已经有了，其中查询支持搜索条件、指定返回字段、排序等等。</p>
<p><img src="./../imgs/0731bc95d135415e9b5ad45fae7a0e6d.png" alt="image.png"> 但是参数也比较复杂，此时可以用到@nestjsx&#x2F;crud-request。这是一个给前端使用的，快速创建查询条件的lib：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">import &#123; RequestQueryBuilder &#125; <span class="keyword">from</span> <span class="string">&#x27;@nestjsx/crud-request&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="variable constant_">queryString</span> = RequestQueryBuilder.<span class="title function_ invoke__">create</span>(&#123;</span><br><span class="line">  <span class="attr">fields</span>: [<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;email&#x27;</span>],</span><br><span class="line">  <span class="attr">search</span>: &#123; <span class="attr">isActive</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">  <span class="attr">join</span>: [&#123; <span class="attr">field</span>: <span class="string">&#x27;company&#x27;</span> &#125;],</span><br><span class="line">  <span class="attr">sort</span>: [&#123; <span class="attr">field</span>: <span class="string">&#x27;id&#x27;</span>, <span class="attr">order</span>: <span class="string">&#x27;DESC&#x27;</span> &#125;],</span><br><span class="line">  <span class="attr">page</span>: <span class="number">1</span>,</span><br><span class="line">  <span class="attr">limit</span>: <span class="number">25</span>,</span><br><span class="line">  <span class="attr">resetCache</span>: <span class="literal">true</span>,</span><br><span class="line">&#125;).<span class="title function_ invoke__">query</span>();</span><br><span class="line"></span><br><span class="line">console.<span class="title function_ invoke__">log</span>(queryString); </span><br><span class="line"><span class="comment">//fields=name%2Cemail&amp;s=%7B%22isActive%22%3Atrue%7D&amp;join%5B0%5D=company&amp;limit=25&amp;page=1&amp;sort%5B0%5D=id%2CDESC&amp;cache=0</span></span><br></pre></td></tr></table></figure>
        
      </div>

         
    </div>
    
     
  </div>
  
    
<nav id="article-nav">
  <a class="article-nav-btn left "
    
      href="/hexo-blog/2022/02/19/UML%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8%E2%80%94%E2%80%94%E7%B1%BB%E5%9B%BE%E5%9F%BA%E7%A1%80/"
      title="UML快速入门——类图基础"
     >
    <i class="fa-solid fa-angle-left"></i>
    <p class="title-text">
      
        UML快速入门——类图基础
        
    </p>
  </a>
  <a class="article-nav-btn right "
    
      href="/hexo-blog/2022/02/16/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E4%B9%8B%E8%AE%A1%E6%95%B0%E6%8E%92%E5%BA%8F/"
      title="排序算法之计数排序"
     >

    <p class="title-text">
      
        排序算法之计数排序
        
    </p>
    <i class="fa-solid fa-angle-right"></i>
  </a>
</nav>


  
</article>





    </div>
    <div id="footer-wrapper">
      <footer id="footer">
  
  <div id="footer-info" class="inner">
    
    &copy; 2024 Zachary<br>
    <!-- Powered by <a href="https://hexo.io/" target="_blank">Hexo</a> & Theme <a target="_blank" rel="noopener" href="https://github.com/saicaca/hexo-theme-vivia">Vivia</a> -->
      <a target="_blank" rel="noopener" href="https://beian.miit.gov.cn">苏ICP备20030005号-2</a>
  </div>
</footer>

    </div>
    <div class="back-to-top-wrapper">
    <button id="back-to-top-btn" class="back-to-top-btn hide" onclick="topFunction()">
        <i class="fa-solid fa-angle-up"></i>
    </button>
</div>

<script>
    function topFunction() {
        window.scroll({ top: 0, behavior: 'smooth' });
    }
    let btn = document.getElementById('back-to-top-btn');
    function scrollFunction() {
        if (document.body.scrollTop > 600 || document.documentElement.scrollTop > 600) {
            btn.classList.remove('hide')
        } else {
            btn.classList.add('hide')
        }
    }
    window.onscroll = function() {
        scrollFunction();
    }
</script>

  </div>
  <script src="/hexo-blog/js/light-dark-switch.js"></script>
</body>
</html>
